<template>
  <div class="leaveHospitalManage">
    <div class="left-menu">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-vertical-demo"
        v-for="item in menuList"
        :key="item.departmrntState"
      >
        <el-menu-item
          :index="item.departmentId.toString()"
          :disabled="item.state"
          @click="menuClick(item.departmentId.toString())"
        >
          <span slot="title">{{ item.departmentName }}</span>
        </el-menu-item>
      </el-menu>
    </div>
    <div class="right">
      <div class="iptRow">
        <el-input v-model="patientName" placeholder="输出患者姓名"></el-input>
        <el-input v-model="phoneNumber" placeholder="输出手机号码"></el-input>
        <el-input v-model="idNumber" placeholder="输出身份证"></el-input>
        <el-select v-model="sex" placeholder="性别">
          <el-option
            v-for="item in sexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button icon="el-icon-search" circle @click="getInHospitalList()"></el-button>
      </div>
      <div class="record">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-link type="success" @click="leaveHospital(scope.row)">出科</el-link>
              <el-link type="primary" @click="transferDepartment(scope.row)">转科</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="patientId" label="用户id"></el-table-column>
          <el-table-column prop="sex" label="性别">
            <template slot-scope="scope">
              <span>{{ scope.row.sex == "1" ? "男" : "女" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="patientName" label="用户姓名"></el-table-column>
          <el-table-column prop="phoneNumber" label="手机号码"></el-table-column>
          <el-table-column prop="idNumber" label="身份证号码"></el-table-column>
          <el-table-column prop="birth" label="出生日期"></el-table-column>
          <el-table-column prop="inquiryDesc" label="备注"></el-table-column>
        </el-table>
      </div>
      <div class="page">
        <el-pagination
          align="right"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="totalNumber"
          style="margin-top: 58px; margin-bottom: 40px"
        ></el-pagination>
      </div>
      <el-dialog
        title="办理出院"
        :visible.sync="dialogVisible2"
        width="30%"
        :before-close="handleClose"
      >
        <el-form ref="form" label-width="80px">
          <el-form-item label="详情">
            <el-input v-model="desc" placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取 消</el-button>
          <el-button type="primary" @click="form2Confirm">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="办理转科" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form ref="form" label-width="80px">
          <el-form-item label="科室列表">
            <el-select v-model="department" placeholder="请选择">
              <el-option
                v-for="item in menuList"
                :key="item.departmentId"
                :label="item.departmentName"
                :value="item.departmentId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="desc" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="formConfirm">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    adminName: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      menuList: [],
      activeIndex: "",
      patientName: "",
      phoneNumber: "",
      idNumber: "",
      sex: "",
      recordId: 0,
      patientId: 0,
      sexOptions: [
        {
          label: "全部",
          value: ""
        },
        {
          label: "男",
          value: "1"
        },
        {
          label: "女",
          value: "0"
        }
      ],
      tableData: [],
      dialogVisible: false,
      dialogVisible2: false,
      department: "",
      departmentId: 0,
      desc: "",
      pageIndex: 1,
      pageSize: 10,
      totalNumber: 100
    };
  },
  created() {
    this.getDepartmentList();
  },
  methods: {
    getDepartmentList() {
      // console.log(111);
      this.$axios.get("/api/CjwDepartment/GetDepartmentList").then(res => {
        this.menuList = res.data;
      });
      console.log(this.menuList);
    },
    getInHospitalList() {
      let params = {
        departmentId: Number(this.activeIndex),
        patientName: this.patientName,
        phoneNumber: this.phoneNumber,
        idNumber: this.idNumber,
        sex: this.sex,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        state: 5
      };
      this.$axios
        .get("/api/CjwPatient/GetPatientRecordList", { params })
        .then(res => {
          this.tableData = res.data.getPatientRecordListDto;
          console.log(this.tableData);
        });
    },
    menuClick(id) {
      this.activeIndex = id;
      console.log(id);
      this.getInHospitalList();
    },
    leaveHospital(item) {
      console.log(item);
      this.desc = "";
      this.departmentId = item.departmentId;
      this.recordId = item.recordId;
      this.dialogVisible2 = true;
    },
    transferDepartment(item) {
      console.log(item);
      this.departmentId = item.departmentId;
      this.recordId = item.recordId;
      this.patientId = item.patientId;
      this.dialogVisible = true;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    formConfirm() {
      let params = {
        patientId: this.patientId,
        inquiryDesc: this.desc,
        departmentId: Number(this.department),
        isTransfer: this.recordId
      };
      console.log(params);
      this.$axios
        .post("/api/CjwPatient/TransferDepartment", params)
        .then(res => {
          this.$message({
            type: "success",
            message: "办理转科成功"
          });
          this.dialogVisible = false;
          this.getInHospitalList();
          let op = {
            opAdminName: this.adminName,
            opTable: "办理转科",
            opData: `记录id:${params.isTransfer}`
          };
          this.$axios.post("/api/CjwOperationList/InsertOpData", op);
          let p1 = {
            departmentId: this.departmentId,
            inOut: 2
          };
          // let p2 = {
          //   departmentId: params.departmentId,
          //   inOut: 1
          // };
          this.$axios.get(
            "/api/CjwInOutList/AddInOut?departmentId=" +
              p1.departmentId +
              "&inOut=" +
              p1.inOut
          );
          // this.$axios.get(
          //   "/api/CjwInOutList/AddInOut?departmentId=" +
          //     p2.departmentId +
          //     "&inOut=" +
          //     p2.inOut
          // );
        });
    },
    form2Confirm() {
      let params = {
        recordId: this.recordId,
        state: 6,
        isTransfer: 0,
        leaveHospitalDesc: this.desc
      };
      console.log(params);
      this.$axios.post("/api/CjwPatient/LeaveHopital", params).then(res => {
        this.$message({
          type: "success",
          message: "出院成功"
        });
        this.dialogVisible2 = false;
        this.getInHospitalList();
        let op = {
          opAdminName: this.adminName,
          opTable: "办理出院",
          opData: `记录id:${params.recordId}`
        };
        this.$axios.post("/api/CjwOperationList/InsertOpData", op);
        let p = {
          departmentId: this.departmentId,
          inOut: 2
        };
        this.$axios.get(
          "/api/CjwInOutList/AddInOut?departmentId=" +
            this.departmentId +
            "&inOut=" +
            p.inOut
        );
      });
    },
    handleCurrentChange() {},
    handleSizeChange() {}
  }
};
</script>

<style scoped>
.leaveHospitalManage {
  display: flex;
}
.left-menu {
  width: 250px;
}
.right {
  width: 100%;
}
.iptRow {
  display: flex;
  margin: 10px;
}
.iptRow .el-input {
  width: 180px;
  padding-right: 20px;
}
.iptRow .el-select {
  padding-right: 20px;
}
.page {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
}
</style>
